
package com.ami.kvm.jviewer.gui;

import com.ami.kvm.jviewer.Debug;
import com.ami.kvm.jviewer.JViewer;
import com.ami.kvm.jviewer.communication.Connection;
import com.ami.kvm.jviewer.videorecord.URLProcessor;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.swing.JOptionPane;

public class SinglePortKVM {
	private String webSessionToken = null;
	private int secWebPort;
	private String tunnelHost = null;
	private int tunnelPort;
	private boolean m_bUseSSL = false;

	private URLProcessor urlProcessor;

	public OutputStream outStream;
	public InputStream inStream;
	public Socket httpsock;
	private String Service = null;
	public static final String VIDEO = "VIDEO";
	public static final String HTTP = "http";
	public static final String HTTPS = "https";
	public SSLSocket sslsock = null;
	public SSLContext context = null;

	public Socket getHttpsock() {
		return this.httpsock;
	}

	public void setHttpsock(Socket paramSocket) {
		this.httpsock = paramSocket;
	}

	public byte[] reply = new byte['È'];

	public OutputStream getOutstream() {
		return this.outStream;
	}

	public void setOutstream(OutputStream paramOutputStream) {
		this.outStream = paramOutputStream;
	}

	public InputStream getInstream() {
		return this.inStream;
	}

	public void setInstream(InputStream paramInputStream) {
		this.inStream = paramInputStream;
	}

	public SSLSocket getSSLsock() {
		return this.sslsock;
	}

	public void setSSLsock(SSLSocket paramSSLSocket) {
		this.sslsock = paramSSLSocket;
	}

	public SinglePortKVM(String paramString, int paramInt1, int paramInt2, boolean paramBoolean) {
		this.tunnelHost = paramString;
		this.tunnelPort = paramInt1;
		this.secWebPort = paramInt2;
		this.m_bUseSSL = paramBoolean;
	}

	public int startConnect() {
		if (0 > setHTTPConnect("VIDEO")) {
			Debug.out.println(LocaleStrings.getString("AE_10_SPKVM"));
			return -1;
		}
		return 0;
	}

	public int getWebSessionToken() {
		JViewerApp.getInstance().setM_webSession_token(null);
		this.urlProcessor = new URLProcessor(null, 1);
		int i = -1;
		String str = null;
		if (JViewer.isRestService() == true) {
			i = this.urlProcessor.restProcessRequest("https://" + this.tunnelHost + ":" + this.secWebPort
					+ "/api/session?username=" + JViewer.getUsername() + "&password=" + JViewer.getPassword());
			try {
				if (i == 0) {
					this.webSessionToken = this.urlProcessor.getValue("Set-Cookie: ", ';');
					JViewerApp.getInstance().setM_webSession_token(this.webSessionToken);
					str = this.urlProcessor.getValue("\"CSRFToken\": ", ' ');
					str.trim();
					URLProcessor.setCsrfToken(str.substring(1, str.length() - 1));
				} else {
					Debug.out.println("getWebSessionToken failed with return value:" + i);
				}
			} catch (Exception localException1) {
				Debug.out.println(localException1);
			}
		} else {
			i = this.urlProcessor.processRequest(
					"https://" + this.tunnelHost + ":" + this.secWebPort + "/rpc/WEBSES/create.asp?WEBVAR_USERNAME="
							+ JViewer.getUsername() + "&WEBVAR_PASSWORD=" + JViewer.getPassword());
			try {
				if (i == 0) {
					this.webSessionToken = this.urlProcessor.getValue("'SESSION_COOKIE' : '", ',');
					this.webSessionToken.trim();
					this.webSessionToken = this.webSessionToken.substring(0, this.webSessionToken.lastIndexOf('\''));
					JViewerApp.getInstance().setM_webSession_token(this.webSessionToken);
				} else {
					Debug.out.println("getWebSessionToken failed with return value:" + i);
				}
			} catch (Exception localException2) {
				Debug.out.println(localException2);
			}
		}
		return i;
	}

	public int setHTTPConnect(String paramString) {
		int i = 0;

		Socket localSocket = null;
		this.Service = paramString;
		setHttpsock(null);
		setOutstream(null);
		setInstream(null);

		if (JViewerApp.getInstance().getM_webSession_token() == null) {
			i = getWebSessionToken();
			if (i < 0)
				return i;
			if (i == 0) {
				JViewerApp.getInstance().getConnectionDialog().setWebLogIn(true);
			}
		}

		try {
			int j = this.m_bUseSSL == true ? 2 : 0;
			localSocket = JViewerApp.getInstance().getConnection().createSocket(InetAddress.getByName(this.tunnelHost),
					this.tunnelPort, j);
			if (localSocket == null) {
				if ((this.m_bUseSSL)
						&& (JViewerApp.getInstance().getConnection().getConnErrCode() == Connection.SUCCESS)) {
					JViewer.exit(0);
				}
				return -1;
			}
			setHttpsock(localSocket);
			setOutstream(localSocket.getOutputStream());
			setInstream(localSocket.getInputStream());
			i = doTunnelHandshake(this.tunnelHost, this.secWebPort);
			if (i < 0) {
				Debug.out.println(LocaleStrings.getString("AE_10_SPKVM"));
			}
		} catch (UnknownHostException localUnknownHostException) {
			Debug.out.println(localUnknownHostException);
			i = -1;
		} catch (IOException localIOException) {
			Debug.out.println(localIOException);
			i = -1;
		} catch (Exception localException) {
			Debug.out.println(localException);
			i = -1;
		}

		if ((JViewer.isStandAloneApp()) || (JViewer.isVMApp()))
			JViewerApp.getInstance().getConnectionDialog().logoutWebSession();
		return i;
	}

	public void DisconnectService(Socket paramSocket) {
		String str = null;
		int i = 0;

		str = "JVIEWER DISCONNECT Cookie " + JViewerApp.getInstance().getM_webSession_token() + "\r\n\r\n";

		if (WriteToConnectedSock(str) == 0) {
			str = null;
		} else {
			Debug.out.println(LocaleStrings.getString("AE_11_SPKVM"));
		}
		try {
			i = ReadFromConnectedSock();
		} catch (IOException localIOException) {
			Debug.out.println(localIOException);
		}
		if (i < 0) {
		}
	}

	private int doTunnelHandshake(String paramString, int paramInt) throws IOException {
		int i = 0;
		String str = null;

		str = FormHttpRequest(" ", paramString, paramInt);

		if (WriteToConnectedSock(str) == 0) {
			str = null;
		} else {
			Debug.out.println(LocaleStrings.getString("AE_10_SPKVM"));
			return -1;
		}

		str = "JVIEWER " + this.Service + " cookie " + JViewerApp.getInstance().getM_webSession_token() + "\r\n\r\n";

		if (WriteToConnectedSock(str) == 0) {
			str = null;
		} else {
			Debug.out.println(LocaleStrings.getString("AE_10_SPKVM"));
			return -1;
		}

		i = ReadFromConnectedSock();
		if (i < 0) {
			return -1;
		}

		return 0;
	}

	private String FormHttpRequest(String paramString1, String paramString2, int paramInt) {
		String str1 = null;
		String str2 = null;

		if (true == this.m_bUseSSL) {
			str2 = " HTTPS/1.1\r\n";
		} else {
			str2 = " HTTP/1.1\r\n";
		}
		str1 = "CONNECT" + paramString1 + paramString2 + ":" + paramInt + str2 + " cookie "
				+ JViewerApp.getInstance().getM_webSession_token() + "\r\n\r\n";

		return str1;
	}

	public String GetErrorCode(String paramString) throws IOException {
		String str = null;
		int i = -1;

		if (null == paramString) {
			return str;
		}
		StringBuffer localStringBuffer = new StringBuffer(paramString);

		for (i = 0; i < localStringBuffer.length(); i++) {
			if (localStringBuffer.charAt(i) == '\\') {
				break;
			}
		}
		try {
			if (i > 0) {
				str = "AE_" + paramString.substring(paramString.indexOf(":") + 1, i) + "_SPKVM";
			}
		} catch (Exception localException) {
			Debug.out.println(localException);
		}

		return str;
	}

	public int ReadFromConnectedSock() throws IOException {
		int i = 0;
		int j = 0;
		int k = 0;
		String str1 = null;

		while (j < 1) {
			int m = getInstream().read();
			if (m < 0) {
				return m;
			}

			if (0 == getInstream().available()) {
				k = 1;
				j++;
			} else if (m != 13) {
				j = 0;
				if ((k == 0) && (i < this.reply.length)) {
					this.reply[(i++)] = ((byte) m);
				}
			}
		}

		String str2 = null;
		try {
			str2 = new String(this.reply, 0, i, "ASCII7");
		} catch (UnsupportedEncodingException localUnsupportedEncodingException) {
			Debug.out.println(localUnsupportedEncodingException);
			str2 = new String(this.reply, 0, i);
		}

		if (str2.contains("ERROR") == true) {
			try {
				str1 = LocaleStrings.getString(GetErrorCode(str2.trim()));
			} catch (Exception localException) {
				Debug.out.println(localException);
				str1 = LocaleStrings.getString("AE_4_SPKVM");
			}
			if (!this.Service.contains("VIDEO")) {
				JOptionPane.showMessageDialog(JViewerApp.getInstance().getMainWindow(), str1,
						LocaleStrings.getString("D_3_JVAPP"), 1);

				return -1;
			}

			JOptionPane.showMessageDialog(JViewerApp.getInstance().getMainWindow(), str1,
					LocaleStrings.getString("AE_10_SPKVM"), 1);

			JViewer.exit(0);
		}
		Debug.out.dump(str2.getBytes());
		return i;
	}

	public int WriteToConnectedSock(String paramString) {
		byte[] arrayOfByte;

		try {
			arrayOfByte = paramString.getBytes("ASCII7");

		} catch (UnsupportedEncodingException localUnsupportedEncodingException) {

			Debug.out.println(localUnsupportedEncodingException);
			arrayOfByte = paramString.getBytes();
		}
		try {
			getOutstream().write(arrayOfByte);
			getOutstream().flush();
		} catch (IOException localIOException) {
			Debug.out.println(localIOException);
			return -1;
		}
		return 0;
	}
}
